<?php
namespace Admin\Controller;
use Common\Controller\AdminController;
class DepController extends AdminController{

	public function index(){
		$list = $this->getList('*', 'dep', null, 'sort desc,id asc',flase,null);
		$this->sortNode($list, $result);
		$this->assign('list', $result);
		$this->display();
	}

	public function add(){
		if(IS_POST){
			$result = $this->updateData($_POST, 'dep');
			$this->returnResult($result);
		}else{
			$list = $this->getList('id,pid,name', 'dep', null, 'sort desc,id asc');
			$this->sortNode($list, $result);
			$this->assign('list', $result);
			$this->display();
		}
	}

	public function edit(){
		if(IS_POST){
			$result = $this->updateData($_POST, 'dep', 2);
			$this->returnResult($result);
		}else{
			$info = $this->getInfo('*', 'dep', 'id=' . I('get.id', 0, 'intval'));
			$this->assign('info', $info);
			$list = $this->getList('id,pid,name', 'dep', null, 'sort desc,id asc');
			$this->sortNode($list, $result);
			$this->assign('list', $result);
			$this->display('add');
		}
	}

	public function del(){
		$id = I('get.id', 0, 'intval');
//		$newsNum = M('news')->where(array('pid' => $id))->count();
//		if($newsNum > 0){
//			$this->returnResult(false, '�����������ţ�����ɾ��');
//		}
		$subNum = M('dep')->where(array('pid' => $id))->count();
		if($subNum > 0){
			$this->returnResult(false, '����ɾ���¼�����');
		}
		$result = $this->deleteData(array('id' => $id), 'dep');
		$this->returnResult($result);
	}

}